

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/fluid.png">
  <link rel="icon" href="/blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Solace">
  <meta name="keywords" content="">
  
    <meta name="description" content="好学爱学">
<meta property="og:type" content="article">
<meta property="og:title" content="flex布局">
<meta property="og:url" content="https://isolace.gitee.io/blog/2023/11/11/flex%E5%B8%83%E5%B1%80/index.html">
<meta property="og:site_name" content="Solace的博客">
<meta property="og:description" content="好学爱学">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://www.runoob.com/wp-content/uploads/2015/07/3791e575c48b3698be6a94ae1dbff79d.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/flex-direction.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/flex-wrap.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/justify-content.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/justify-content(2).png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/align-items.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/align-content.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/gap.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/order.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/flex-grow.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/flex-shrink.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/flex-basis.png">
<meta property="og:image" content="https://isolace.gitee.io/blog/post_img/flex%E5%B8%83%E5%B1%80/align-self.png">
<meta property="article:published_time" content="2023-11-11T10:28:31.000Z">
<meta property="article:modified_time" content="2024-03-26T11:57:16.627Z">
<meta property="article:author" content="Solace">
<meta property="article:tag" content="css">
<meta property="article:tag" content="布局">
<meta property="article:tag" content="web">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://www.runoob.com/wp-content/uploads/2015/07/3791e575c48b3698be6a94ae1dbff79d.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>flex布局 - Solace的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"isolace.gitee.io","root":"/blog/","version":"1.9.5-a","typing":{"enable":true,"typeSpeed":60,"cursorChar":"|","loop":false,"scope":["home"]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  

  

  
    <!-- Google tag (gtag.js) -->
    <script async>
      if (!Fluid.ctx.dnt) {
        Fluid.utils.createScript("https://www.googletagmanager.com/gtag/js?id=", function() {
          window.dataLayer = window.dataLayer || [];
          function gtag() {
            dataLayer.push(arguments);
          }
          gtag('js', new Date());
          gtag('config', '');
        });
      }
    </script>
  

  

  

  

  



  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>Solace&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle">flex布局</span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        Solace
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-11-11 18:28" pubdate>
          2023年11月11日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          11k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          89 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">flex布局</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="flex布局"><a href="#flex布局" class="headerlink" title="flex布局"></a>flex布局</h1><h3 id="一、是什么？"><a href="#一、是什么？" class="headerlink" title="一、是什么？"></a>一、是什么？</h3><p>弹性布局，为盒模型提供最大的灵活性</p>
<p>任一容器都可指定为flex布局，行内元素也可以使用：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-class">.box</span>&#123;<br>	<span class="hljs-attribute">display</span>:flex;<br>&#125;<br><span class="hljs-selector-class">.box</span>&#123;<br>	<span class="hljs-attribute">display</span>: inline-flex;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>设为Flex布局以后，子元素的float、clear和vertical-align属性将失效。</p>
<h3 id="二、基本概念"><a href="#二、基本概念" class="headerlink" title="二、基本概念"></a>二、基本概念</h3><p>采用flex布局的元素称为<strong>flex容器</strong>，简称容器；<br>所有子元素自动成为容器成员，称为<strong>flex项目</strong>，简称项目。</p>
<img src="https://www.runoob.com/wp-content/uploads/2015/07/3791e575c48b3698be6a94ae1dbff79d.png" srcset="/blog/img/loading.gif" lazyload alt="flex简介" class='post_img' />

<p>容器默认存在两根轴：水平的<strong>主轴</strong>和垂直于主轴的<strong>交叉轴</strong>；</p>
<p>主轴&#x2F;交叉轴的开始位置（与容器边框的交点）为<code>main start</code>&#x2F;<code>cross start</code>，结束位置为<code>main end</code>&#x2F;<code>cross end</code>；</p>
<p>项目默认按主轴排列，单个项目占据的主轴&#x2F;交叉轴空间叫做<code>main size</code>&#x2F;<code>cross size</code>。</p>
<h3 id="三、容器属性"><a href="#三、容器属性" class="headerlink" title="三、容器属性"></a>三、容器属性</h3><blockquote>
<p><strong>注：</strong></p>
<ol>
<li><p>基本样式如下</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-class">.d1</span>&#123;<br>    <span class="hljs-attribute">width</span>:<span class="hljs-number">100px</span>;<br>    <span class="hljs-attribute">height</span>:<span class="hljs-number">100px</span>;<br>    <span class="hljs-attribute">border</span>:<span class="hljs-number">3px</span> black solid;<br>    <span class="hljs-attribute">margin</span>:<span class="hljs-number">10px</span>;<br>    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;<br>    <span class="hljs-attribute">text-align</span>: center;<br>    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">100px</span>;<br>&#125;<br><span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[id^=<span class="hljs-string">&#x27;big&#x27;</span>]</span>&#123;<br>    <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span>;<br>    <span class="hljs-attribute">border</span>:<span class="hljs-built_in">rgb</span>(<span class="hljs-number">243</span>, <span class="hljs-number">121</span>, <span class="hljs-number">141</span>) <span class="hljs-number">3px</span> solid;<br>    <span class="hljs-attribute">display</span>: flex;<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>html结构采用emmet语法结构，如：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!--  div#big$*2&gt;div.d1&#123;$&#125;*3  的结构为--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big1&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big2&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure></li>
</ol>
</blockquote>
<p>以下6个属性设置在容器上：(点击跳转)</p>
<ul>
<li><a href="#1-flex-direction"><span>flex-direction</span></a>（决定主轴方向）</li>
<li><a href="#2-flex-wrap"><span>flex-wrap</span></a>（决定换行方式）</li>
<li><a href="#3-flex-flow"><span>flex-flow</span></a>（以上两个属性的简写）</li>
<li><a href="#4-justify-content"><span>justify-content</span></a>（项目在主轴上的对齐方式）</li>
<li><a href="#5-align-items"><span>align-items</span></a>（项目在交叉轴上的对齐方式）</li>
<li><a href="#6-align-content"><span>align-content</span></a>（决定多根轴线的对齐方式）</li>
<li><a href="#7-place-content"><span>place-content</span></a>（<code>justify-content</code>和<code>align-content</code>的简写）</li>
<li><a href="#8-row-gap"><span>row-gap</span></a>（设置行之间的间隙大小）</li>
<li><a href="#9-column-gap"><span>column-gap</span></a>（设置列之间的间隙大小）</li>
<li><a href="#10-gap"><span>gap</span></a>（<code>row-gap</code>和<code>column-gap</code>的简写）</li>
</ul>
<h4 id="1-flex-direction"><a href="#1-flex-direction" class="headerlink" title="1. flex-direction"></a>1. flex-direction</h4><p><strong>作用</strong>：定义主轴方向</p>
<p><strong>取值</strong>：</p>
<ul>
<li><code>row</code>：主轴水平，起点在上；</li>
<li><code>row-reverse</code>：主轴水平，起点在下；</li>
<li><code>column</code>：主轴垂直，起点在上；</li>
<li><code>column-reverse</code>：主轴垂直，起点在下。</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-comment">/* flex样式 */</span></span><br><span class="language-css">    <span class="hljs-selector-id">#big1</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-direction</span>: row;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-direction</span>: row-reverse;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big3</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-direction</span>: column;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big4</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-direction</span>: column-reverse;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-comment">&lt;!-- html结构（下同）：</span><br><span class="hljs-comment">div#big$*4&gt;div.d1&#123;$&#125;*3</span><br><span class="hljs-comment">--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big1&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big2&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big3&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big4&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/flex-direction.png" srcset="/blog/img/loading.gif" lazyload alt="flex-direction示意" style="max-width:80%;" class='post_img' />

<h4 id="2-flex-wrap"><a href="#2-flex-wrap" class="headerlink" title="2. flex-wrap"></a>2. flex-wrap</h4><p><strong>作用：</strong>默认情况的项目都排在一条线（轴线）上，该属性定义如果一条轴线排不下时如何换行</p>
<p><strong>取值：</strong></p>
<ul>
<li><code>nowrap</code>（默认）：不换行；</li>
<li><code>wrap</code>：换行，第一行在上方；</li>
<li><code>wrap-reverse</code>：换行，第一行在下方。</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-id">#big1</span> &#123;  <span class="hljs-comment">/*flex-wrap: nowrap;*/</span>   &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> &#123;  <span class="hljs-attribute">flex-wrap</span>: wrap;         &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big3</span> &#123;  <span class="hljs-attribute">flex-wrap</span>: wrap-reverse; &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-comment">&lt;!-- html结构：</span><br><span class="hljs-comment">div#big$*3&gt;div.d1&#123;$&#125;*9</span><br><span class="hljs-comment">--&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/flex-wrap.png" srcset="/blog/img/loading.gif" lazyload alt="flex-wrap示意" style="max-width:90%;" class='post_img'/>

<h4 id="3-flex-flow"><a href="#3-flex-flow" class="headerlink" title="3. flex-flow"></a>3. flex-flow</h4><p><strong>作用：</strong>是<code>flex-direction属性</code>和<code>flex-wrap属性</code>的简写形式，默认值为<code>row nowrap</code></p>
<h4 id="4-justify-content"><a href="#4-justify-content" class="headerlink" title="4. justify-content"></a>4. justify-content</h4><p><strong>作用：</strong>定义项目在主轴的对齐方式</p>
<p><strong>取值：</strong></p>
<ul>
<li><code>flex-start</code>（默认值）：从主轴起始位置开始排列；</li>
<li><code>flex-end</code>：从主轴结束位置开始排列；</li>
<li><code>center</code>：居于主轴中间；</li>
<li><code>space-between</code>：两端的项目紧贴主轴的两端，其余项目间间隔相等；</li>
<li><code>space-around</code>：每个项目两侧间隔相等，即两项目间距等于项目与边框距离的两倍；</li>
<li><code>space-evenly</code>：项目与项目之间、以及项目与边框之间距离相等。</li>
</ul>
<img src="../../../../post_img/flex布局/justify-content.png" srcset="/blog/img/loading.gif" lazyload alt="justify-content示意" style="max-width:98%;" class='post_img'/>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-id">#big1</span> &#123; <span class="hljs-attribute">justify-content</span>: flex-start;    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> &#123; <span class="hljs-attribute">justify-content</span>: flex-end;      &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big3</span> &#123; <span class="hljs-attribute">justify-content</span>: center;        &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big4</span> &#123; <span class="hljs-attribute">justify-content</span>: space-between; &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big5</span> &#123; <span class="hljs-attribute">justify-content</span>: space-evenly;  &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big6</span> &#123; <span class="hljs-attribute">justify-content</span>: space-around;  &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-comment">&lt;!-- html结构：</span><br><span class="hljs-comment">div#big$*6&gt;div.d1&#123;$&#125;*5</span><br><span class="hljs-comment">--&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/justify-content(2).png" srcset="/blog/img/loading.gif" lazyload alt="justify-content示意" style="max-width:80%;" class='post_img'/>

<h4 id="5-align-items"><a href="#5-align-items" class="headerlink" title="5. align-items"></a>5. align-items</h4><p><strong>作用：</strong>定义项目在交叉轴的对齐方式</p>
<p><strong>取值：</strong></p>
<ul>
<li><code>stretch</code>（默认值）：如果项目未设置高度或为auto，则项目高度占满容器高度；</li>
<li><code>flex-start</code>：从交叉轴起始位置开始排列；</li>
<li><code>flex-end</code>：从交叉轴结束位置开始排列；</li>
<li><code>center</code>：居于交叉轴中间；</li>
<li><code>baseline</code>：与项目第一行文字的基线对齐。</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[class^=<span class="hljs-string">&#x27;d&#x27;</span>]</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>:<span class="hljs-number">100px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">border</span>:<span class="hljs-number">3px</span> black solid;</span><br><span class="language-css">        <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">text-align</span>: center;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[id^=<span class="hljs-string">&#x27;big&#x27;</span>]</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">border</span>:<span class="hljs-built_in">rgb</span>(<span class="hljs-number">243</span>, <span class="hljs-number">121</span>, <span class="hljs-number">141</span>) <span class="hljs-number">3px</span> solid;</span><br><span class="language-css">        <span class="hljs-attribute">display</span>: flex;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">200px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big4</span> <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:auto;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-id">#big1</span> &#123;  <span class="hljs-attribute">align-items</span>: flex-start;  &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> &#123;  <span class="hljs-attribute">align-items</span>: flex-end;    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big3</span> &#123;  <span class="hljs-attribute">align-items</span>: center;      &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big4</span> &#123;  <span class="hljs-attribute">align-items</span>: stretch;     &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big5</span> &#123;  <span class="hljs-attribute">align-items</span>: baseline;    &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.d1</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">40px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">line-height</span>: <span class="hljs-number">40px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.d2</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">60px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">line-height</span>: <span class="hljs-number">60px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.d3</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">80px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">line-height</span>: <span class="hljs-number">80px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.d4</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">100px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">line-height</span>: <span class="hljs-number">100px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">div#big$*5&gt;div.d1&#123;$&#125;*4 </span><br><span class="hljs-comment">--&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/align-items.png" srcset="/blog/img/loading.gif" lazyload alt="align-items示意" style="max-width:80%;" class='post_img' />

<h4 id="6-align-content"><a href="#6-align-content" class="headerlink" title="6. align-content"></a>6. align-content</h4><p><strong>作用：</strong>定义了存在多跟轴线（<code>flex-wrap</code>换行）时项目的对齐方式；</p>
<blockquote>
<p>如果项目只有一根轴线，则该属性不起作用</p>
</blockquote>
<p>取值：</p>
<ul>
<li><code>flex-start</code>：与交叉轴起点对齐；</li>
<li><code>flex-end</code>：与交叉轴终点对齐；</li>
<li><code>center</code>：居与交叉轴的中部；</li>
<li><code>space-between</code>：两端轴线上的项目紧贴交叉轴的两端，其余轴线间间隔相等；</li>
<li><code>space-around</code>：每条轴线两侧间隔相等，即两轴线间距等于轴线与边框距离的两倍；</li>
<li><code>space-evenly</code>：轴线与轴线之间、以及轴线与边框之间距离相等。</li>
<li><code>stretch</code>：轴线上的项目占满整个交叉轴；</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!--删除默认样式中&#x27;.d1&#x27;的height使stretch生效--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[id^=<span class="hljs-string">&#x27;big&#x27;</span>]</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-wrap</span>:wrap;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">450px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big1</span> &#123; <span class="hljs-attribute">align-content</span>: flex-start;    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> &#123; <span class="hljs-attribute">align-content</span>: flex-end;      &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big3</span> &#123; <span class="hljs-attribute">align-content</span>: center;        &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big4</span> &#123; <span class="hljs-attribute">align-content</span>: space-between; &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big5</span> &#123; <span class="hljs-attribute">align-content</span>: space-evenly;  &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big6</span> &#123; <span class="hljs-attribute">align-content</span>: space-around;  &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big7</span> &#123; <span class="hljs-attribute">align-content</span>: stretch;       &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">div#big$*7&gt;div.d1&#123;$&#125;*7</span><br><span class="hljs-comment">--&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/align-content.png" srcset="/blog/img/loading.gif" lazyload alt="align-content示意" style="max-width:80%;" class='post_img' />

<h4 id="7-place-content"><a href="#7-place-content" class="headerlink" title="7. place-content"></a>7. place-content</h4><p><strong>作用：</strong><code>justify-content</code>和<code>align-content</code>的简写；</p>
<blockquote>
<p>如果没有设置第二个值，那么第二个的值与第一个相等，此前提是第一个值对两个属性都是有效的。</p>
</blockquote>
<h4 id="8-row-gap"><a href="#8-row-gap" class="headerlink" title="8. row-gap"></a>8. row-gap</h4><p><strong>作用：</strong>定义行间间隙大小</p>
<p><strong>取值：</strong>&lt;length&gt;或百分数</p>
<p>详见下<code>gap</code>属性</p>
<h4 id="9-column-gap"><a href="#9-column-gap" class="headerlink" title="9. column-gap"></a>9. column-gap</h4><p><strong>作用：</strong>定义列间间隙大小</p>
<p><strong>取值：</strong>&lt;length&gt;或百分数</p>
<p>详见下<code>gap</code>属性</p>
<h4 id="10-gap"><a href="#10-gap" class="headerlink" title="10. gap"></a>10. gap</h4><p><strong>作用：</strong><code>row-gap</code>和<code>column-gap</code>的简写；</p>
<p><strong>取值：</strong>&lt;length&gt;或百分数</p>
<blockquote>
<p>如果没有设置第二个值，那么第二个的值与第一个相等，此前提是第一个值对两个属性都是有效的。</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[id^=<span class="hljs-string">&#x27;big&#x27;</span>]</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-wrap</span>: wrap;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:auto;</span><br><span class="language-css">        <span class="hljs-attribute">margin</span>:<span class="hljs-number">50px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[class^=<span class="hljs-string">&#x27;d&#x27;</span>]</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">margin</span>:<span class="hljs-number">0px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>:<span class="hljs-number">180px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">gap</span>: <span class="hljs-number">20px</span> <span class="hljs-number">30px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big1&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d4&quot;</span>&gt;</span>4<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d5&quot;</span>&gt;</span>5<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d6&quot;</span>&gt;</span>6<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big2&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d4&quot;</span>&gt;</span>4<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d5&quot;</span>&gt;</span>5<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d6&quot;</span>&gt;</span>6<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/gap.png" srcset="/blog/img/loading.gif" lazyload alt="gap示意" style="max-width:80%;" class='post_img' />

<h3 id="四、项目属性"><a href="#四、项目属性" class="headerlink" title="四、项目属性"></a>四、项目属性</h3><ul>
<li><a href='#1-order'><span>order</span></a>（定义项目顺序）</li>
<li><a href='#2-flex-grow'><span>flex-grow</span></a>（定义项目放大比例）</li>
<li><a href='#3-flex-shrink'><span>flex-shrink</span></a>（定义项目缩小比例）</li>
<li><a href='#4-flex-basis'><span>flex-basis</span></a>（在分配多余空间之前项目占用的主轴空间）</li>
<li><a href='#5-flex'><span>flex</span></a>（<code>flex-grow</code>、<code>flex-shrink</code>、<code>flex-basis</code>的简写）</li>
<li><a href='#6-align-self'><span>align-self</span></a>（定义单个项目的对齐方式）</li>
</ul>
<blockquote>
<p><strong>注：</strong></p>
<ol>
<li><p>本部分的css基本样式代码：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs css"><span class="hljs-selector-tag">div</span>&#123;<br>    <span class="hljs-attribute">margin</span>:<span class="hljs-number">10px</span>;<br>&#125;<br><span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[class^=<span class="hljs-string">&#x27;d&#x27;</span>]</span>&#123;<br>    <span class="hljs-attr">--height</span>:<span class="hljs-number">80px</span>;<br>    <span class="hljs-attribute">width</span>:<span class="hljs-number">150px</span>;<br>    <span class="hljs-attribute">height</span>:<span class="hljs-built_in">var</span>(--height);<br>    <span class="hljs-attribute">border</span>:<span class="hljs-number">3px</span> black solid;<br>    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;<br>    <span class="hljs-attribute">text-align</span>: center;<br>    <span class="hljs-attribute">line-height</span>: <span class="hljs-built_in">var</span>(--height);<br>&#125;<br><span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[id^=<span class="hljs-string">&#x27;big&#x27;</span>]</span>&#123;<br>    <span class="hljs-attribute">width</span>:<span class="hljs-number">700px</span>;<br>    <span class="hljs-attribute">height</span>:<span class="hljs-number">150px</span>;<br>    <span class="hljs-attribute">border</span>:<span class="hljs-built_in">rgb</span>(<span class="hljs-number">243</span>, <span class="hljs-number">121</span>, <span class="hljs-number">141</span>) <span class="hljs-number">3px</span> solid;<br>    <span class="hljs-attribute">display</span>: flex;<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>没了</p>
</li>
</ol>
</blockquote>
<h4 id="1-order"><a href="#1-order" class="headerlink" title="1. order"></a>1. order</h4><p><strong>作用：</strong>定义项目的排列顺序；</p>
<p><strong>取值：</strong>整数（负数也可以），数值越小，排列越靠前；默认为0。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-class">.d1</span> &#123;  <span class="hljs-attribute">order</span>: <span class="hljs-number">2</span>;   &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.d2</span> &#123;  <span class="hljs-attribute">order</span>: <span class="hljs-number">3</span>;   &#125;</span><br><span class="language-css">    <span class="hljs-selector-class">.d3</span> &#123;  <span class="hljs-attribute">order</span>: <span class="hljs-number">1</span>;   &#125; <span class="hljs-comment">/* 默认值 */</span></span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big1&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/order.png" srcset="/blog/img/loading.gif" lazyload alt="order示意" style="max-width:80%;" class='post_img' />

<h4 id="2-flex-grow"><a href="#2-flex-grow" class="headerlink" title="2. flex-grow"></a>2. flex-grow</h4><p><strong>作用：</strong>容器存在剩余空间时，定义项目瓜分剩余空间的比例；</p>
<p><strong>取值：</strong>非负数，默认为0（意为即使存在剩余空间也不会瓜分）</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> <span class="hljs-selector-class">.d1</span> &#123;  </span><br><span class="language-css">        <span class="hljs-attribute">flex-grow</span>: <span class="hljs-number">1</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> <span class="hljs-selector-class">.d2</span> &#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-grow</span>: <span class="hljs-number">2</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big1&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big2&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/flex-grow.png" srcset="/blog/img/loading.gif" lazyload alt="flex-grow示意" style="max-width:80%;" class='post_img' />

<h4 id="3-flex-shrink"><a href="#3-flex-shrink" class="headerlink" title="3. flex-shrink"></a>3. flex-shrink</h4><p><strong>作用：</strong>项目存在溢出空间时，定义项目的压缩程度；</p>
<p><strong>取值：</strong>非负数，默认为1</p>
<ul>
<li>0：意为即使存在溢出空间也不压缩项目尺寸</li>
</ul>
<blockquote>
<p><strong>注：</strong></p>
<ol>
<li>溢出空间：项目尺寸之和大于容器尺寸时二者的差值；</li>
<li>不存在溢出空间（项目尺寸之和不大于容器尺寸）时，设置flex-shrink无效。</li>
</ol>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[id^=<span class="hljs-string">&#x27;big&#x27;</span>]</span>  &#123; <span class="hljs-attribute">width</span>:<span class="hljs-number">500px</span>; &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[class^=<span class="hljs-string">&#x27;d&#x27;</span>]</span> &#123; <span class="hljs-attribute">width</span>:<span class="hljs-number">200px</span>; &#125;</span><br><span class="language-css">    <span class="hljs-comment">/* 容器尺寸500px，小于项目尺寸之和600px */</span></span><br><span class="language-css">    <span class="hljs-comment">/* 存在溢出空间，此时flex-shrink生效 */</span></span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> <span class="hljs-selector-tag">div</span> &#123; <span class="hljs-attribute">flex-shrink</span>:<span class="hljs-number">0</span>;&#125; <span class="hljs-comment">/* 无压缩 */</span></span><br><span class="language-css">    <span class="hljs-selector-id">#big3</span> <span class="hljs-selector-class">.d1</span> &#123; <span class="hljs-attribute">flex-shrink</span>:<span class="hljs-number">1</span>; &#125; </span><br><span class="language-css">    <span class="hljs-selector-id">#big3</span> <span class="hljs-selector-class">.d2</span> &#123; <span class="hljs-attribute">flex-shrink</span>:<span class="hljs-number">1.5</span>; &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big3</span> <span class="hljs-selector-class">.d3</span> &#123; <span class="hljs-attribute">flex-shrink</span>:<span class="hljs-number">1.5</span>; &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big1&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big2&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big3&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/flex-shrink.png" srcset="/blog/img/loading.gif" lazyload alt="flex-shrink示意" style="max-width:80%;" class='post_img' />



<h4 id="4-flex-basis"><a href="#4-flex-basis" class="headerlink" title="4. flex-basis"></a>4. flex-basis</h4><p><strong>作用：</strong>设置项目在主轴方向上的<strong>初始大小</strong>，即项目内容盒<code>content-box</code>的大小（不使用<code>box-sizing</code>改变盒模型的前提下）</p>
<p><strong>取值：</strong></p>
<ul>
<li><p>非负&lt;length&gt;（&lt;number&gt; + 单位）</p>
</li>
<li><p>百分数（相对于容器主轴尺寸）</p>
</li>
<li><p>auto（默认），未设置width或height（主轴方向所占尺寸）时尺寸依据其中的内容（max-content）</p>
</li>
</ul>
<blockquote>
<p><strong>优先级：</strong><code>max/min-width</code> &gt; <code>flex-basis</code>(除auto) &gt; <code>width</code></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> <span class="hljs-selector-class">.d1</span> &#123;</span><br><span class="language-css">        <span class="hljs-attribute">min-width</span>: <span class="hljs-number">150px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">100px</span>; <span class="hljs-comment">/*不生效*/</span></span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> <span class="hljs-selector-class">.d2</span> &#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">100px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;  <span class="hljs-comment">/* 不生效 */</span></span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> <span class="hljs-selector-class">.d3</span> &#123;</span><br><span class="language-css">        <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">200px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">flex-grow</span>:<span class="hljs-number">1</span>; <span class="hljs-comment">/* 200px + 容器剩余空间 */</span></span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big1&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big2&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/flex-basis.png" srcset="/blog/img/loading.gif" lazyload alt="flex-basis示意" style="max-width:80%;" class='post_img' />

<h4 id="5-flex"><a href="#5-flex" class="headerlink" title="5. flex"></a>5. flex</h4><p><strong>作用：</strong><code>flex-grow</code>、<code>flex-shrink</code>和<code>flex-basis</code>的简写；</p>
<p><strong>取值：</strong>默认值<code>0 1 auto</code></p>
<ul>
<li><p>快捷值（关键字值）：</p>
<ul>
<li><p>auto（1 1 auto）：有溢出空间时压缩，有剩余空间时伸长；</p>
</li>
<li><p>none（0 0 auto）：既不压缩也不伸长；</p>
</li>
<li><p>initial（默认值）（0 1 auto）：溢出时压缩，剩余时不伸长</p>
</li>
</ul>
</li>
<li><p>单值</p>
<ul>
<li>无单位:表示<code>flex-grow</code>，此时<code>flex-basis</code>为0；</li>
<li>尺寸（宽高）值：表示<code>flex-basis</code><ul>
<li>&lt;length&gt;</li>
<li>min&#x2F;max-content</li>
</ul>
</li>
</ul>
</li>
<li><p>双值</p>
<ul>
<li>第一个值是<code>flex-grow</code></li>
<li>第二个值<ul>
<li>无单位值：<code>flex-shrink</code>；</li>
<li>尺寸（宽高）值：<code>flex-basis</code>。</li>
</ul>
</li>
</ul>
</li>
<li><p>三值：依次为<code>flex-grow</code>、<code>flex-shrink</code>和<code>flex-basis</code></p>
</li>
</ul>
<h4 id="6-align-self"><a href="#6-align-self" class="headerlink" title="6. align-self"></a>6. align-self</h4><p><strong>作用：</strong>改变单个项目在交叉轴上的对齐方式；</p>
<p><strong>取值：</strong>同<code>align-items</code>；</p>
<ul>
<li>auto（默认）：继承父元素的<code>align-items</code>属性，没有父元素则等同于<code>stretch</code></li>
</ul>
<blockquote>
<p>覆盖该项目已有的<code>align-items</code>的值</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span><span class="hljs-selector-attr">[id^=<span class="hljs-string">&#x27;big&#x27;</span>]</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">250px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-id">#big2</span> <span class="hljs-selector-class">.d2</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">align-self</span>: flex-end;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big1&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;big2&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d1&quot;</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d2&quot;</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;d3&quot;</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<img src="../../../../post_img/flex布局/align-self.png" srcset="/blog/img/loading.gif" lazyload alt="align-self示意" style="max-width:80%;" class='post_img' />

<hr>
<blockquote>
<p>推荐网站：</p>
<ol>
<li><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_flexible_box_layout/Controlling_ratios_of_flex_items_along_the_main_axis">控制弹性元素在主轴上的比例-MDN</a></li>
<li><a target="_blank" rel="noopener" href="https://linxz.github.io/flex_learn_manual/">从点到面学习 flex 弹性布局 (linxz.github.io)</a></li>
</ol>
</blockquote>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><p>[1] <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/">MDN Web Docs</a></p>
<p>[2] 《CSS新世界》 张鑫旭 著</p>
<p>[3] <a target="_blank" rel="noopener" href="http://www.ruanyifeng.com/blog/">阮一峰的网络日志</a></p>
<style>
    *{
        scroll-behavior:smooth;
    }
    .post_img{
        position:relative;
        left:50%;
        transform:translate(-50%,0);
    }
</style>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/css/" class="print-no-link">#css</a>
      
        <a href="/blog/tags/%E5%B8%83%E5%B1%80/" class="print-no-link">#布局</a>
      
        <a href="/blog/tags/web/" class="print-no-link">#web</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>flex布局</div>
      <div>https://isolace.gitee.io/blog/2023/11/11/flex布局/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Solace</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年11月11日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2023/11/13/JS%E7%BA%A2%E5%AE%9D%E4%B9%A6%E7%AC%AC%E4%B8%80%E3%80%81%E4%BA%8C%E7%AB%A0%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" title="JS红宝书第一、二章学习笔记">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">JS红宝书第一、二章学习笔记</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2023/03/20/%E5%88%9D%E7%AB%A0/" title="初章">
                        <span class="hidden-mobile">初章</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="twikoo"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#comments', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/twikoo/1.6.8/twikoo.all.min.js', function() {
        var options = Object.assign(
          {"envId":"https://imaginative-rabanadas-4bf3ac.netlify.app/.netlify/functions/twikoo","region":"AWS / N. Virginia (us-east-1)","path":"window.location.pathname"},
          {
            el: '#twikoo',
            path: 'window.location.pathname',
            onCommentLoaded: function() {
              Fluid.utils.listenDOMLoaded(function() {
                var imgSelector = '#twikoo .tk-content img:not(.tk-owo-emotion)';
                Fluid.plugins.imageCaption(imgSelector);
                Fluid.plugins.fancyBox(imgSelector);
              });
            }
          }
        )
        twikoo.init(options)
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  


  
  









    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>Solace</span><br> <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>





  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  
      <script>
        if (!window.MathJax) {
          window.MathJax = {
            tex    : {
              inlineMath: { '[+]': [['$', '$']] }
            },
            loader : {
              load: ['ui/lazy']
            },
            options: {
              renderActions: {
                insertedScript: [200, () => {
                  document.querySelectorAll('mjx-container').forEach(node => {
                    let target = node.parentNode;
                    if (target.nodeName.toLowerCase() === 'li') {
                      target.parentNode.classList.add('has-jax');
                    }
                  });
                }, '', false]
              }
            }
          };
        } else {
          MathJax.startup.document.state(0);
          MathJax.texReset();
          MathJax.typeset();
          MathJax.typesetPromise();
        }

        Fluid.events.registerRefreshCallback(function() {
          if ('MathJax' in window && MathJax.startup.document && typeof MathJax.startup.document.state === 'function') {
            MathJax.startup.document.state(0);
            MathJax.texReset();
            MathJax.typeset();
            MathJax.typesetPromise();
          }
        });
      </script>
    

  <script  src="https://lib.baomitu.com/mathjax/3.2.2/es5/tex-mml-chtml.js" ></script>

  <script  src="/blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
